package com.eshop.tools.rest;

import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.Map;
import com.eshop.tools.domain.QiniuContent;
import java.util.HashMap;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.data.domain.Pageable;
import java.io.IOException;
import com.eshop.tools.service.dto.QiniuQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.tools.domain.QiniuConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.eshop.tools.service.QiNiuService;
import org.slf4j.Logger;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "/api/qiNiuContent" })
@Api(tags = { "工具：七牛云存储管理" })
public class QiniuController
{
    private static final Logger log;
    private final QiNiuService qiNiuService;
    
    public QiniuController(final QiNiuService qiNiuService) {
        this.qiNiuService = qiNiuService;
    }
    
    @GetMapping({ "/config" })
    public ResponseEntity<Object> get() {
        return (ResponseEntity<Object>)new ResponseEntity((Object)this.qiNiuService.find(), HttpStatus.OK);
    }
    
    @Log("配置七牛云存储")
    @ApiOperation("配置七牛云存储")
    @PutMapping({ "/config" })
    public ResponseEntity<Object> emailConfig(@Validated @RequestBody final QiniuConfig qiniuConfig) {
        this.qiNiuService.update(qiniuConfig);
        this.qiNiuService.update(qiniuConfig.getType());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping({ "/download" })
    public void download(final HttpServletResponse response, final QiniuQueryCriteria criteria) throws IOException {
        this.qiNiuService.downloadList(this.qiNiuService.queryAll(criteria), response);
    }
    
    @Log("查询文件")
    @ApiOperation("查询文件")
    @GetMapping
    public ResponseEntity<Object> getRoles(final QiniuQueryCriteria criteria, final Pageable pageable) {
        return (ResponseEntity<Object>)new ResponseEntity(this.qiNiuService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    
    @Log("上传文件")
    @ApiOperation("上传文件")
    @PostMapping
    public ResponseEntity<Object> upload(@RequestParam final MultipartFile file) {
        final QiniuContent qiniuContent = this.qiNiuService.upload(file, this.qiNiuService.find());
        final Map<String, Object> map = new HashMap<String, Object>(3);
        map.put("id", qiniuContent.getId());
        map.put("errno", 0);
        map.put("data", new String[] { qiniuContent.getUrl() });
        return (ResponseEntity<Object>)new ResponseEntity((Object)map, HttpStatus.OK);
    }
    
    @Log("同步七牛云数据")
    @ApiOperation("同步七牛云数据")
    @PostMapping({ "/synchronize" })
    public ResponseEntity<Object> synchronize() {
        this.qiNiuService.synchronize(this.qiNiuService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("下载文件")
    @ApiOperation("下载文件")
    @GetMapping({ "/download/{id}" })
    public ResponseEntity<Object> download(@PathVariable final Long id) {
        final Map<String, Object> map = new HashMap<String, Object>(1);
        map.put("url", this.qiNiuService.download(this.qiNiuService.findByContentId(id), this.qiNiuService.find()));
        return (ResponseEntity<Object>)new ResponseEntity((Object)map, HttpStatus.OK);
    }
    
    @Log("删除文件")
    @ApiOperation("删除文件")
    @DeleteMapping({ "/{id}" })
    public ResponseEntity<Object> delete(@PathVariable final Long id) {
        this.qiNiuService.delete(this.qiNiuService.findByContentId(id), this.qiNiuService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("删除多张图片")
    @ApiOperation("删除多张图片")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody final Long[] ids) {
        this.qiNiuService.deleteAll(ids, this.qiNiuService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    static {
        log = LoggerFactory.getLogger((Class)QiniuController.class);
    }
}
